<template>
  <div class="pb50">
    <el-form ref="form" size="small" :model="form" label-width="200px">
      <div class="common-form">{{ $t('user.points_settings') }}</div>
      <el-form-item :label="$t('user.point_name_zh')" prop="pointsName" :rules="[{ required: true, message: ' ' }]">
        <el-input v-model="form.pointsName" autocomplete="off" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item :label="$t('user.point_name_hk')" prop="pointsName" :rules="[{ required: true, message: ' ' }]">
        <el-input v-model="form.pointsNameHk" autocomplete="off" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item :label="$t('user.point_name_en')" prop="pointsName" :rules="[{ required: true, message: ' ' }]">
        <el-input v-model="form.pointsNameEn" autocomplete="off" class="max-w460"></el-input>
        <div class="lh18 mt10 gray9">
          <p> {{ $t('user.points_name_tip') }}

          </p>
          <p> {{ $t('user.points_name_example') }}

          </p>
        </div>
      </el-form-item>
      <el-form-item :label="$t('user.points_description')" :rules="[{ required: true, message: ' ' }]">
        <el-input class="max-w460" type="textarea" rows="5" v-model="form.describe" autocomplete="off"></el-input>
      </el-form-item>
      <div class="common-form">{{ $t('user.points_give') }}</div>
      <el-form-item :label="$t('user.enable_order_give_points')">
        <el-radio-group v-model="form.isShoppingGift">
          <el-radio :label="1">{{ $t('user.enabled') }}</el-radio>
          <el-radio :label="0">{{ $t('user.disabled') }}</el-radio>
        </el-radio-group>
        <div class="lh18 mt10 gray9">
          <p>{{ $t('user.order_give_points_note') }}</p>
          <p> {{ $t('user.order_give_points_rule') }}

          </p>
        </div>
      </el-form-item>
      <el-form-item :label="$t('user.points_give_ratio')" prop="giftRatio" :rules="[{ required: true, message: ' ' }]">
        <el-input :placeholder="$t('user.input_content')" v-model="form.giftRatio" class="max-w460">
          <template #append>%</template>
        </el-input>
        <div class="lh18 mt10 gray9">
          <p>{{ $t('user.points_give_ratio_note') }}</p>
          <p> {{ $t('user.points_give_ratio_example') }}


          </p>
        </div>
      </el-form-item>
      <div class="common-form">{{ $t('user.points_deduction') }}</div>
      <el-form-item :label="$t('user.enable_points_deduction')">
        <el-radio-group v-model="form.isShoppingDiscount" class="max-w460">
          <el-radio :label="1">{{ $t('user.allowed') }}</el-radio>
          <el-radio :label="0">{{ $t('user.not_allowed') }}</el-radio>
        </el-radio-group>
        <div class="lh18 mt10 gray9">
          <p>{{ $t('user.points_deduction_note') }}</p>
        </div>
      </el-form-item>
      <el-form-item :label="$t('user.enable_points_to_balance')">
        <el-radio-group v-model="form.isTransBalance" class="max-w460">
          <el-radio :label="1">{{ $t('user.allowed') }}</el-radio>
          <el-radio :label="0">{{ $t('user.not_allowed') }}</el-radio>
        </el-radio-group>
        <div class="lh18 mt10 gray9">
          <p>{{ $t('user.points_to_balance_note') }}</p>
        </div>
      </el-form-item>
      <el-form-item :label="$t('user.points_deduction_ratio')">
        <el-input :placeholder="$t('user.input_content')" v-model="form.discount.discountRatio" class="max-w460">
          <template #prepend>{{ $t('user.points_deduction_value') }}</template>
          <template #append>{{ $t('user.currency_yuan') }}</template>
        </el-input>
        <div class="lh18 mt10 gray9">
          <p> {{ $t('user.points_deduction_example') }}

          </p>
        </div>
      </el-form-item>
      <el-form-item :label="$t('user.deduction_conditions')">
        <el-input :placeholder="$t('user.input_content')" v-model="form.discount.fullOrderPrice" class="max-w460">
          <template #prepend>{{ $t('user.order_min_amount') }}</template>
          <template #append>{{ $t('user.currency_yuan') }}</template>
        </el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-input :placeholder="$t('user.input_content')" v-model="form.discount.maxMoneyRatio" class="max-w460">
          <template #prepend>{{ $t('user.max_deductible_amount') }}</template>
          <template #append>%</template>
        </el-input>
        <div class="lh18 mt10 gray9">
          <p>{{ $t('user.deduction_tip') }}</p>
        </div>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" size="small" @click="onSubmit" :loading="loading">{{ $t('user.submit') }}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import PointsApi from "@/api/points.js";
export default {
  data() {
    return {
      form: {
        pointsName: "",
        describe: "",
        isShoppingGift: 0,
        giftRatio: 10,
        isShoppingDiscount: 0,
        discount: {
          discountRatio: 0,
          fullOrderPrice: 0,
          maxMoneyRatio: 0
        },
        isTransBalance: 0
      },
      loading: false
    };
  },
  created() {
    /*获取数据*/
    this.getData();
  },
  methods: {
    /*获取数据*/
    getData() {
      let self = this;
      let Params = {};
      PointsApi.getPoints(Params, true).then(res => {
        self.form = res.data;
        self.form.isShoppingGift = res.data.isShoppingGift;
        self.form.isShoppingDiscount = res.data.isShoppingDiscount;
        self.form.isTransBalance = res.data.isTransBalance;
      }).catch(error => { });
    },
    /*保存*/
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          PointsApi.setPoints(form, true).then(data => {
            self.loading = false;
            if (data.code == 1) {
              ElMessage({
                message: $t("user.save_success"),
                type: "success"
              });
            } else {
              self.loading = false;
            }
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    }
  }
};
</script>
